package wwy.Service;

import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import wwy.Dao.BookshelfDao;
import wwy.Dto.BookDetailDto;
import wwy.Dto.BookShelfDto;
import wwy.Entity.Book;
import wwy.Entity.Bookshelf;

import java.util.*;

@Service
public class BookDetailService extends LogicService<BookshelfDao, Bookshelf,Long>{
    private final HomeService homeService;
    public BookDetailService(@Autowired BookshelfDao dao, HomeService homeService) {
        super(dao);
        this.homeService = homeService;
    }

    @Transactional
    public BookDetailDto getBookDetail(String bookName){
        BookDetailDto bookDetailDto = new BookDetailDto();
        Book bookInfo = homeService.getBookInfo(bookName);
        bookDetailDto.setBookName(bookInfo.getBookName());
        bookDetailDto.setBookAuthor(bookInfo.getBookAuthor());
        bookDetailDto.setBookCover(bookInfo.getBookCover());
        bookDetailDto.setBookRatings(bookInfo.getBookRatings());
        bookDetailDto.setBookType(bookInfo.getBookType());
        bookDetailDto.setBookAbstract(bookInfo.getBookAbstract());
        bookDetailDto.setProgress(dao.findProgressByBookId(bookInfo.getId()));
        return bookDetailDto;
    }

    @Transactional
    public Double getBookProgress(String bookName){
        Book bookInfo = homeService.getBookInfo(bookName);
        return dao.findProgressByBookId(bookInfo.getId());
    }

    @Transactional
    public void setBookProgress(String bookName, Double progress){
        Book book = homeService.getBookInfo(bookName);
        Long bookShelfId = dao.findBookshelfIdByBookId(book.getId());
        Bookshelf bookshelf = dao.getReferenceById(bookShelfId);
        bookshelf.setProgress(progress);
        dao.save(bookshelf);
    }
    @Transactional
    public List<BookShelfDto> getBookShelf(){
        List<BookShelfDto> bookShelfDtoS = new ArrayList<>();
        List<Bookshelf> bookshelfList = dao.findAll();
        for(Bookshelf bookshelf : bookshelfList){
            BookShelfDto bookShelfDto = new BookShelfDto();
            String bookName = homeService.getBookById(bookshelf.getBooks().iterator().next().getId()).getBookName();
            bookShelfDto.setBookName(bookName);
            bookShelfDto.setAuthor(homeService.getBookAuthor(bookName));
            bookShelfDto.setBookCover(homeService.getBookCover(bookName));
            bookShelfDto.setProgress(bookshelf.getProgress());
            bookShelfDtoS.add(bookShelfDto);
        }
        return bookShelfDtoS;
    }

    @Transactional
    @EventListener(ApplicationReadyEvent.class)
    @Order(2)
    public void addDefaultBookShelf(){
        if(dao.count() > 0) return;
        Bookshelf bookshelf1 = new Bookshelf();
        Book book1 = homeService.getBook("杀死一只知更鸟");
        bookshelf1.setBooks(new HashSet<>(Collections.singletonList(book1)));
        bookshelf1.setProgress(12.0);
        dao.save(bookshelf1);

        Bookshelf bookshelf2 = new Bookshelf();
        Book book2 = homeService.getBook("霍乱时期的爱情");
        bookshelf2.setBooks(new HashSet<>(Collections.singletonList(book2)));
        bookshelf2.setProgress(60.0);
        dao.save(bookshelf2);

        Bookshelf bookshelf3 = new Bookshelf();
        Book book3 = homeService.getBook("穷爸爸富爸爸");
        bookshelf3.setBooks(new HashSet<>(Collections.singletonList(book3)));
        bookshelf3.setProgress(30.0);
        dao.save(bookshelf3);

        Bookshelf bookshelf4 = new Bookshelf();
        Book book4 = homeService.getBook("被讨厌的勇气");
        bookshelf4.setBooks(new HashSet<>(Collections.singletonList(book4)));
        bookshelf4.setProgress(10.0);
        dao.save(bookshelf4);

        Bookshelf bookshelf5 = new Bookshelf();
        Book book5 = homeService.getBook("我与地坛");
        bookshelf5.setBooks(new HashSet<>(Collections.singletonList(book5)));
        bookshelf5.setProgress(0.0);

    }
}
